import { BufferGeometry3D, DodecahedronGeometry3D } from 'feng3d-three';
import { BufferGeometryUtils } from '../thirdparty/three/imports';

export class BufferGeometryUtils3D
{
    static mergeVertices<T extends BufferGeometry3D>(geometry: T, tolerance?: number): T
    {
        const bufferGeometry = BufferGeometryUtils.mergeVertices(geometry._geometry, tolerance);

        let bufferGeometry3D: T;
        if (geometry.constructor === DodecahedronGeometry3D)
        {
            bufferGeometry3D = DodecahedronGeometry3D.get(bufferGeometry as any) as any as T;
        }
        else
        {
            bufferGeometry3D = BufferGeometry3D.get(bufferGeometry) as T;
        }

        return bufferGeometry3D;
    }

    static mergeBufferGeometries<T extends BufferGeometry3D>(geometries: T[], useGroups?: boolean)
    {
        const _geometries = geometries.map((v) => v._geometry);

        const bufferGeometry = BufferGeometryUtils.mergeBufferGeometries(_geometries, useGroups);

        const bufferGeometry3D = BufferGeometry3D.get(bufferGeometry) as T;

        return bufferGeometry3D;
    }
}
